package com.jhm.bing_fa_rong_qi;

import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;

public class CopyOnWriteArraySetExample {
    public static void main(String[] args) {
        // 线程安全的CopyOnWriteArraySet
        Set<String> set = new CopyOnWriteArraySet<>();

        // 添加元素
        set.add("Element 1");
        set.add("Element 2");
        set.add("Element 3");

        // 遍历集合
        System.out.println("Set elements:");
        for (String element : set) {
            System.out.println(element);
        }

        // 尝试添加重复元素
        boolean added = set.add("Element 2");
        System.out.println("Added duplicate element: " + added);

        // 检查元素是否存在
        boolean contains = set.contains("Element 3");
        System.out.println("Contains 'Element 3': " + contains);

        // 删除元素
        boolean removed = set.remove("Element 1");
        System.out.println("Removed 'Element 1': " + removed);

        // 再次遍历集合
        System.out.println("Set elements after removal:");
        for (String element : set) {
            System.out.println(element);
        }

        // 清空集合
        set.clear();
        System.out.println("Set size after clear: " + set.size());
    }
}